import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:wallet/entity/quotation_entity.dart';
import 'package:wallet/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:wallet/http/http.dart';
import 'package:wallet/widget/BaseViewBar.dart';

class QuotationPage extends StatefulWidget {
  @override
  _QuotationPageState createState() => _QuotationPageState();
}

class _QuotationPageState extends State<QuotationPage> {
  List<QuotationData> data;

  @override
  void initState() {
    super.initState();
    loadData();
  }

  void loadData() async {
    String url =
        "https://mdncapi.bqiapp.com/api/coin/web-coinrank?webp=1&pagesize=10&page=1&type=-1";
    Http.get(HttpRequest(url, success: (res) {
      QuotationEntity entity = QuotationEntity();
      entity = entity.fromJson(res);
      setState(() {
        data = entity.data;
      });
    }));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: BaseViewBar(
        preferredSize: Size.fromHeight(50.0),
        child: BaseTitleBar(
          S.of(context).quotation,
          showLeading: false,
        ),
      ),
      body: Container(
        child: data == null
            ? Center(
                child: Text("加载中..."),
              )
            : ListView.builder(
                itemCount: data.length,
                itemBuilder: (BuildContext context, int index) {
                  return buildItem(context, index);
                }),
      ),
    );
  }

  Widget buildItem(BuildContext context, int index) {
    QuotationData it = data[index];
    return Container(
      margin: EdgeInsets.fromLTRB(10, 10, 10, 0),
      padding: EdgeInsets.all(10),
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(5))),
      child: Row(
        children: <Widget>[
          Image.network(it.logo),
          SizedBox(width: 20),
          Container(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(it.name + "/USDT"),
                Text(
                  "24H ${(it.volUsd / it.currentPriceUsd).toStringAsFixed(2)}",
                  style: TextStyle(color: Colors.black54, fontSize: 12),
                )
              ],
            ),
          ),
          Expanded(child: Text("")),
          Container(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: <Widget>[
                Text("${it.currentPrice}"),
                Text(
                  "${it.currentPriceUsd}",
                  style: TextStyle(fontSize: 12, color: Colors.black54),
                )
              ],
            ),
          ),
          SizedBox(
            width: 20,
          ),
          Container(
            width: 80,
            height: 30,
            alignment: Alignment.center,
            child: Text(
              "${it.changePercent.toStringAsFixed(2)}%",
              style: TextStyle(color: Colors.white),
            ),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(15)),
                color: it.changePercent > 0 ? Colors.green : Colors.red),
          )
        ],
      ),
    );
  }
}
